<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分销协议列表</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" media="all">
    <style>
        .procotol-container {
            padding: 20px;
        }

        .procotol-container {
            background-color: #fff;
        }
        .procotol-container form {
            width: 800px;
            margin: 0 auto;
            padding: 30px;
            border: 1px solid #eee;
        } 

        .procotolList {
            padding: 20px;
        }

        td[data-field="imgUrl"] div {
            height: 120px;
        }

        .lay-form {
            padding: 20px;
        }

        .procotolList {
            background-color: #fff;
        }
        .layui-layedit-iframe iframe {
            height: 400px !important;
        }
    </style>
</head>

<body>
    <div class="procotolList">
        <table lay-filter="procotolListTable" class="layui-hide" id="procotolListTable"></table>
    </div>

</body>

<!-- 添加 -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>

<!-- 操作 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

<script src="https://www.layuicdn.com/layui-v2.5.6/layui.js"></script>
<script src="../utils.js"></script>
<script src="../addProduct/layedit.js"></script>
<script>
    layui.use(['table', 'upload', 'form', 'jquery', 'layedit'], function () {
        var table = layui.table,
            upload = layui.upload,
            form = layui.form,
            layedit = layui.layedit,
            $ = layui.jquery;
            var editContext = "";

        table.render({
            elem: '#procotolListTable'
            , toolbar: "#toolbarDemo"
            , url: window.baseUrl + '/distribution/getProtolList'
            , method: 'POST'
            , page: true
            , contentType: 'application/json'
            , request: {
                pageName: 'start' //页码的参数名称，默认：page
                , limitName: 'length' //每页数据量的参数名，默认：limit
            }
            , headers: {
                Authorization: localStorage.token
            }
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 200,
                    "count": res.data.recordsTotal, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            }
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            }
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [[
                { field: 'id', width: 80, title: 'ID', sort: true }
                , { field: 'procotolContent', title: '协议内容' }
                , {
                    field: 'status', title: '是否启用', templet: function (res) {
                        return '<input type="checkbox" '+ (res.status == 1 ? 'checked=""' : "") +'" lay-filter="switchTest" data-id="'+ res.id +'" value="'+ res.status +'" lay-skin="switch" lay-text="启用|未启用" />'
                    }
                }
                , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 80 }
            ]]
        });

        // 添加
        table.on('toolbar(procotolListTable)', function (obj) {
            switch (obj.event) {
                case 'add':
                    //页面层
                    $("input[name=id]").val("");
                    showLayer();
                    break;
            };
        });

        // 开关
        form.on("switch(switchTest)", function(e) {
            ajax({
                path: "/distribution/saveOrUpdateProcotol",
                data: {
                    id: $(e.elem).attr("data-id"),
                    status: e.value == "1" ? 0 : 1
                },
                success: function (res) {
                    layer.msg("更新成功");
                    table.reload("procotolListTable");
                }
            }, $)

            // setTimeout(function() {
            //     window.location.reload();
            // }, 1000)
        })

        // 编辑
        table.on("tool(procotolListTable)", function (data) {
            switch (data.event) {
                case "edit":
                    showLayer(data.data);
                    form.val('procotol', data.data);
                    break;
            }
        })


        function showLayer(data) {
            layer.open({
                type: 1,
                title: "添加/编辑协议信息",
                area: ['960px', '699px'], //宽高
                content: `
                    <div class="procotol-container">
                        <form class="layui-form" lay-filter="procotol">
                            <fieldset class="layui-elem-field layui-field-title">
                                <legend>添加/编辑分销协议</legend>
                            </fieldset>

                            <div class="layui-form-item">
                                <label class="layui-form-label">协议内容</label>
                                <div class="layui-input-block">
                                    <textarea placeholder="请输入内容" id="procotol" name="procotolContent" class="layui-textarea"></textarea>
                                </div>
                            </div>

                            <input type="hidden" name="id" />
                            <input type="hidden" name="status" />


                            <!-- <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
                                <legend>状态</legend>
                            </fieldset>

                            <div class="layui-form-item">
                                <label class="layui-form-label">是否应用</label>
                                <div class="layui-input-block">
                                    <input type="checkbox" checked="" name="status" lay-skin="switch" lay-text="是|否">
                                </div>
                            </div> --> 
                    
                            
                            <div class="layui-form-item" style="text-align:center">
                                <div class="layui-input-block" style="margin-left: 0;">
                                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="addprocotol">立即提交</button>
                                    <!-- <button type="reset" class="layui-btn layui-btn-primary">重置</button> -->
                                </div>
                            </div>
                        </form>
                    </div> `,
                    success() {
                        console.log(data);
                        // 富文本配置
                        if (layedit) {
                            editContext = layedit.build('procotol', {
                                uploadImage: {
                                    url: window.baseUrl + '/file/uploadFile', 
                                    type: 'post'
                                }
                            });
                            setTimeout(function() {
                                if (layedit.setContent) {
                                    layedit.setContent(editContext, data.procotolContent);
                                }
                                
                            }, 500)
                            
                        }
                        
                    }
            });

            form.render();

            // form 提交
            // form.on("submit(addprocotolListSubmit)", function (el) {
            //     // 有id后台就更新，没协议就添加
            //     ajax({
            //         path: "/distribution/saveOrUpdateProcotol",
            //         data: el.field,
            //         success: function (res) {
            //             layer.closeAll();
            //             layer.msg("操作成功!");
            //             table.reload("procotolListTable");
            //         }
            //     }, $)
            //     return false;
            // })

            // 提交
            form.on("submit(addprocotol)", function(obj) {

                if (!obj.field.id) {
                    delete obj.field.id
                }

                obj.field.procotolContent = layedit.getContent(editContext)
                
                obj.field.status = obj.field.status ? 1 : 0;

                if (!obj.field.procotolContent) {
                    layer.msg("请填写协议内容在进行提交");
                    return false;
                }
 
                ajax({
                    path: "/distribution/saveOrUpdateProcotol",
                    data: obj.field,
                    success: function (res) {
                        layer.closeAll();
                        layer.msg("操作成功");
                        table.reload("procotolListTable");
                    }
                }, $)

                return false;
            })
        }
    });
</script>

</html>